<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport( 'joomla.application.component.model' );

class NvModelKota extends JModel
{

	var $_id = null;
	var $_data = null;

	function __construct(){
		parent::__construct();
		global $mainframe;
		$array = JRequest::getVar('cid', array(0), '', 'array');
		$array2 = JRequest::getVar('pid', array(0), '', 'array');		

		$this->setId((int)$array[0],(int)$array2[0]);
		
		$limit = $mainframe->getUserStateFromRequest( 'limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
		$limitstart = $mainframe->getUserStateFromRequest( 'limitstart', 'limitstart', 0, 'int' );
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);		
	}

	function setId($kota_id,$propinsi_id){
		// Set weblink id and wipe data
		$this->_id1		= $kota_id;
		$this->_id2		= $propinsi_id;
		$this->_data	= null;
		
		
	}

	function getData(){
	
		global $mainframe,$option;
		$search	= $mainframe->getUserStateFromRequest( $option.'search','search','','string' );
		$search	= JString::strtolower( $search );		
		
		//$where = array();
		if ($search) {
			$where = 'and nama_kota LIKE '.$this->_db->Quote( '%'.$this->_db->getEscaped( $search, true ).'%', false );
		}
		
		$task = JRequest::getVar('task');
		
		$query_view = ' 
		SELECT a.kota_id, a.nama_kota, b.nama_propinsi,a.propinsi_id '
		. ' FROM #__kota a, #__propinsi b '
		. ' WHERE a.propinsi_id = b.propinsi_id '
		. $where
		. ' ORDER BY b.propinsi_id ';		
		
		$query_edit =  ' 
		SELECT * FROM #__kota'
		. ' WHERE kota_id = '.$this->_id1 
		. ' and propinsi_id ='.$this->_id2 ;				
		if (!isset($task) || empty($task) || !empty( $this->_data )) {	
			$this->_data = $this->_getList( $query_view, $this->getState('limitstart'), $this->getState('limit') );	
			//echo $query_view;
			return $this->_data;
			
		} 
		else {
			if ( empty($this->_data ) ){
				
				$this->_db->setQuery($query_edit);
				$this->_data = $this->_db->loadObject();				
			} else {
				$this->_data = new stdClass();
				$this->_data->id = 0;
				
				$items = array('kota_id','propinsi_id','nama_kota');
				
				foreach ($items as $item) {
					$this->_data->$item = null;
				}					
			}				
			return $this->_data;					
		}
	}	
	
	function getTotal(){
		$query_view = ' 
		SELECT a.kota_id, a.nama_kota, b.nama_propinsi,a.propinsi_id '
		. ' FROM #__kota a, #__propinsi b '
		. ' WHERE a.propinsi_id = b.propinsi_id '
		. ' ORDER BY b.propinsi_id '
		;		
		if (empty($this->_total)){
			$this->_total = $this->_getListCount( $query_view );
		}
		return $this->_total;
	}
	
	function getPagination(){
		if (empty($this->_pagination)){
			jimport('joomla.html.pagination');
			$this->_pagination = new JPagination( $this->getTotal(), $this->getState('limitstart'), $this->getState('limit') );
		}
		return $this->_pagination;
	}	
	
	function store($post){
		$row =& $this->getTable();
		$data = JRequest::get( 'post');
		echo 'angga';
		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		//cek kondisi apakah form berasal dari input atau edit
		if ($data['doTask'] == 'take'){
			
			$ambilNilaiMax = $this->getAmbilNilaiMax($data['propinsi_id']);
			$kota_id = $ambilNilaiMax->nilai_max+1; // id kota untuk nama kota yang di inputkan
		
			$query = '
			INSERT INTO #__kota VALUES ( null ,
			'.$kota_id.', 
			'.$post['propinsi_id'].','.
			$this->_db->Quote($data['nama_kota'])	
			.')';	
			echo $query;
			$this->_db->setQuery($query);
			
			if(!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}			
		}		
		// -------------------- set query update nilai kota ----------------//
		else {				
			$query = '
			UPDATE #__kota SET nama_kota = "'.$data['nama_kota'].'"			
			WHERE kota_id = '. $data['kota_ids'] .' and propinsi_id = '. $data['propinsi_ids'] .'
			';			
			$this->_db->setQuery($query);			
			$this->_db->query();
			
			if(!$this->_db->query()) {
				$this->setError($this->_db->getErrorMsg());
				return false;
			}		
		}		
		return true;
	}

	/**
	 *  Mengembalikan nilai id kota yang tertinggi untuk selanjutnya di tambah 1 
	 *  supaya id kota tidak bertabrakan
	 */
	function getAmbilNilaiMax($propinsi_id){
		$query = ' 
		SELECT max( kota_id ) as nilai_max '
		. ' FROM #__kota '
		. ' WHERE propinsi_id = '.$propinsi_id
		;
		//echo $query;
		$this->_db->setQuery( $query );
		$this->_data = $this->_db->loadObject();
	
		return $this->_data;
	}

	function delete(){
		$array = JRequest::getVar('cid', array(0), '', 'array');
		if (count( $array )){
			foreach($array as $cid) {			
				$id = explode("+", $cid);
				$propinsi_id 	= $id[0];
				$kota_id 		= $id[1];
				$query = 'DELETE FROM #__kota WHERE kota_id='.$kota_id .' and propinsi_id='.$propinsi_id  ;
				
				$this->_db->setQuery($query);			
				$this->_db->query();
				
				if(!$this->_db->query()) {
					$this->setError($this->_db->getErrorMsg());
					return false;
				}	
			}				
		}
		return true;
	}	
}
